package util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "Filter0",
        urlPatterns = {"/*"}/*仅所有资源进行过滤*/)
public class Filter implements javax.servlet.Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws ServletException, IOException {
        System.out.println("Filter 0 - encoding begins");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String method = request.getMethod();
        //delete设置响应字符集
        response.setContentType("application/json;charset=UTF-8");
        //设置请求字符集
        request.setCharacterEncoding("UTF-8");

        //执行其他过滤器，如已执行完毕，则执行原请求
        chain.doFilter(req, resp);
        System.out.println("Filter 0 - encoding ends");
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
